package com.dwx.guli.service.vod.service.impl;


import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.dwx.guli.service.base.exception.GuliException;
import com.dwx.guli.service.base.result.ResultCodeEnum;
import com.dwx.guli.service.vod.service.VideoService;
import com.dwx.guli.service.vod.util.AliyunVodSDKUtils;
import com.dwx.guli.service.vod.util.VodProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.InputStream;
import java.util.List;

@Slf4j
@Service
public class VideoServiceImpl implements VideoService {
    @Autowired
    VodProperties vodProperties;
    @Override
    public String uploadVideo(InputStream file, String originalFilename) {
        String keyId = vodProperties.getKeyId();
        String keySecret = vodProperties.getKeySecret();
        String templateGroupId = vodProperties.getTemplateGroupId();
        String workflowId = vodProperties.getWorkflowId();
        String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));
        UploadStreamRequest request = new UploadStreamRequest(keyId, keySecret, title, originalFilename,file);


        /* 模板组ID(可选) */
        //request.setTemplateGroupId(templateGroupId);
        /* 工作流ID(可选) */
        request.setWorkflowId(workflowId);

        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        System.out.print("RequestId=" + response.getRequestId() + "\n");  //请求视频点播服务的请求ID
        String videoId = response.getVideoId();
        if (!StringUtils.isEmpty(videoId)) {
            return videoId;
        } else {
            log.error("阿里云上传失败：" + response.getCode() + " - " + response.getMessage());
            throw new GuliException(ResultCodeEnum.VIDEO_UPLOAD_ALIYUN_ERROR);
        }
    }

    @Override
    public void removeVideo(String videoSourceId) throws ClientException {
        String keyId = vodProperties.getKeyId();
        String keySecret = vodProperties.getKeySecret();
        DefaultAcsClient client= AliyunVodSDKUtils.initVodClient(keyId,keySecret);
        DeleteVideoRequest request = new DeleteVideoRequest();
        request.setVideoIds(videoSourceId);
        client.getAcsResponse(request);
    }

    @Override
    public void removeVideoByIdList(List<String> videoIdList) throws ClientException {
        String keyId = vodProperties.getKeyId();
        String keySecret = vodProperties.getKeySecret();
        DefaultAcsClient client= AliyunVodSDKUtils.initVodClient(keyId,keySecret);
        DeleteVideoRequest request = new DeleteVideoRequest();
        int size = videoIdList.size();
        StringBuffer idListStr=new StringBuffer();
        for (int i = 0; i <size ; i++) {
            idListStr.append(videoIdList.get(i));
            if(i==size-1||i%20==19){
                request.setVideoIds(idListStr.toString());
                idListStr=new StringBuffer();
            }else if(i%20<19){
                idListStr.append(",");
            }
        }
    }

    @Override
    public String getPlayAuth(String videoSourceId) throws ClientException {
        String keyId = vodProperties.getKeyId();
        String keySecret = vodProperties.getKeySecret();
        DefaultAcsClient client =AliyunVodSDKUtils.initVodClient(
                keyId, keySecret);
        GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
        request.setVideoId(videoSourceId);
        GetVideoPlayAuthResponse response = client.getAcsResponse(request);
        return response.getPlayAuth();
    }
}
